local skel = fk.CreateSkill {
  name = "efengqi__tonglie",
  tags = {Skill.Compulsory},
  dynamic_desc = function (self, player, lang)
    local removed = player:getTableMark("efengqi__tonglie_removed")
    if #removed > 0 then
      local args = {}
      for i = 1, 3 do
        if not table.contains(removed, "efengqi__tonglie"..i) then
          table.insert(args, "efengqi__tonglie"..i)
        else
          table.insert(args, "efengqi__tonglie_removed")
        end
      end
      return "efengqi__tonglie_dyn:" .. table.concat(args, ":")
    end
  end,
}

Fk:loadTranslationTable{
  ["efengqi__tonglie"] = "统烈",
  [":efengqi__tonglie"] = "锁定技，你于出牌阶段使用的：首张牌无法被响应；前两张牌无次数限制；前三张牌无距离限制。结束阶段，你可以"..
  "交出一项效果并将手牌摸至四张。",

  [":efengqi__tonglie_dyn"] = "锁定技，你于出牌阶段使用的：{1}；{2}；{3}。结束阶段，你可以交出一项效果并将手牌摸至四张。",

  ["efengqi__tonglie_active"] = "统烈",
  ["#efengqi__tonglie-give"] = "统烈：你可以将一项效果交给其他角色并将手牌摸至四张",
  ["@[efengqi__tonglie]"] = "统烈",
  ["efengqi__tonglie_removed"] = "(已移除)",

  ["efengqi__tonglie1"] = "首张牌无法被响应",
  ["efengqi__tonglie2"] = "前两张牌无次数限制",
  ["efengqi__tonglie3"] = "前三张牌无距离限制",
  ["abbr_efengqi__tonglie1"] = "响",
  ["abbr_efengqi__tonglie2"] = "次",
  ["abbr_efengqi__tonglie3"] = "距",

  ["$efengqi__tonglie1"] = "猛将之烈，统帅之所往。",
  ["$efengqi__tonglie2"] = "与子龙忠勇相往，猛烈相合。",
}

local function hasTL(player, i)
  return table.contains(player:getTableMark("@[efengqi__tonglie]"), "efengqi__tonglie"..i) or
  (player:hasSkill(skel.name) and not table.contains(player:getTableMark("efengqi__tonglie_removed"), "efengqi__tonglie"..i))
end

skel:addEffect(fk.CardUsing, { -- 首张牌无法被响应
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player.phase == Player.Play  then
      local first = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        return e.data.from == player
      end, Player.HistoryPhase)[1]
      if first and first.data == data then
        return hasTL(player, 1)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = table.simpleClone(player.room.players)
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if target == player and player.phase == Player.Finish and player:hasSkill(skel.name) then
      return #player:getTableMark("efengqi__tonglie_removed") < 3 and #player.room:getOtherPlayers(player, false) > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player, { skill_name = "efengqi__tonglie_active", prompt = "#efengqi__tonglie-give"})
    if success and dat then
      local choice = dat.interaction
      local to = dat.targets[1]
      room:doIndicate(player, {to})
      room:addTableMark(player, "efengqi__tonglie_removed", choice)
      if #to:getTableMark("efengqi__tonglie_removed") > 0 then
        room:removeTableMark(to, "efengqi__tonglie_removed", choice)
      else
        room:addTableMark(to, "@[efengqi__tonglie]", choice)
      end
      if player:getHandcardNum() < 4 then
        player:drawCards(4 - player:getHandcardNum(), skel.name)
      end
    end
  end,
})

skel:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "efengqi__tonglie-phase", 1) -- 用于给状态技记录使用牌数
  end,
})

skel:addEffect(fk.PreCardUse, { -- 不计入次数
  can_refresh = function(self, event, target, player, data)
    if target == player and player.phase == Player.Play and player:getMark("efengqi__tonglie-phase") < 2 then
      return hasTL(player, 2)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    data.extraUse = true
  end,
})

skel:addEffect("targetmod", {
  bypass_distances =  function(self, player, skill)
    if hasTL(player, 3) then
      return player.phase == Player.Play and player:getMark("efengqi__tonglie-phase") < 3
    end
  end,
  bypass_times = function(self, player, skill, scope)
    if hasTL(player, 2) then
      return player.phase == Player.Play and player:getMark("efengqi__tonglie-phase") < 2
    end
  end,
})

-- 因为这个效果可以给别人发，所以也得监测一下别人的用牌数
skel:addAcquireEffect(function (self, player, is_start)
  local p = table.find(Fk:currentRoom().alive_players, function (p)
    return p.phase == Player.Play
  end)
  if p then -- 补充记录器
    player.room:setPlayerMark(p, "efengqi__tonglie-phase",
    #player.room.logic:getEventsOfScope(GameEvent.UseCard, 999, function (e)
      return e.data.from == p
    end, Player.HistoryPhase))
  end
end)

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "efengqi__tonglie_removed", 0)
end)

Fk:addQmlMark{
  name = "efengqi__tonglie",
  how_to_show = function(name, value)
    if type(value) == "table" and #value > 0 then
      local list = {}
      for i = 1, 3 do
        if table.contains(value, "efengqi__tonglie" .. i) then
          table.insert(list, Fk:translate("abbr_efengqi__tonglie" .. i, "zh_CN"))
        end
      end
      return table.concat(list, " ")
    end
    return " "
  end,
  qml_path = "packages/aaa_fengqi/qml/Tonglie"
}

return skel
